//获取应用实例
const app = getApp();

Page({
  /**
  * 页面的初始数据
  */
  data: {
    orderList1:[],
    orderList1isNull: false,
    page: 1,
    size: 30,
    hasMoreData: false,
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function () {
    
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo();
  },

  onShow:function(){
    var that=this;
    that.onPullDownRefresh();
    // that.setData({
    //   orderList1:[],
    // })
    // that.getOrder1();
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

//待付款
  getOrder1: function () {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that=this;
    console.log(app.globalData.token)
    console.log(that.data.page)
    console.log(that.data.size)
    wx.request({
      url: app.globalData.api + '/app.php?c=Order&a=getOrderList',
      data: {
        token: app.globalData.token,
        p: that.data.page,
        per: that.data.size,
        status: 1,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'post',
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if(res.data.code==0){
          var contentlistTem = res.data.data.list;
          // that.data.orderList1 = [];
          // if (res.data.data.list.length == 0) {
          //   console.log("noData")
          //   that.setData({
          //     orderList1isNull: true,
          //     hasMoreData:false
          //   })
          // } else {
            for (var i = 0; i < res.data.data.list.length; i++) {
              if (res.data.data.list[i].detail.length > 0) {
                // if (res.data.data.list[i].detail[0].status == 1) {
                res.data.data.list[i].detail[0].img = app.globalData.imgUrl + "" + res.data.data.list[i].detail[0].img;
                that.setData({
                  orderList1: that.data.orderList1.concat(res.data.data.list[i]),
                })
                // } 
              }
            }
          // }
          if (that.data.orderList1.length > 0) {
            if (contentlistTem.length < that.data.size) {
              that.setData({
                hasMoreData: false
              })
            } else {
              that.setData({
                hasMoreData: true
              })
            }
            that.setData({
              orderList1isNull: false
            })
          } else {
            that.setData({
              orderList1isNull: true,
              hasMoreData: false
            })
          }
        }else{
            wx.showToast({
              title:""+res.data.msg,
              icon:'none'
            })
        }
      }
    })
    
  },


  //取消订单
  cancelOrder: function (e) {
    // console.log(e)
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/cancel',
      data: {
        token: app.globalData.token,
        order_id: '' + e.currentTarget.dataset.id,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if (res.data.code == 1) {
          // that.getOrder1();
          that.onPullDownRefresh();
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },

  //获取支付表单
  getPayForm(e) {
    var that = this;
    wx.showLoading({
      title: '支付中',
    })
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/getPayForm',
      data:
      {
        token: app.globalData.token,
        order_id: e.currentTarget.dataset.id,
        pay_method: '' +app.globalData.payfrom,
        open_id: app.globalData.openid
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.code == 1) {
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 1000
          })
          that.onPullDownRefresh();
          // that.statistics();
          // "{"appid":"wx48126316bdbec25b","noncestr":"lk8qhri54a7stxryu88e4n5tuhgc0f7f","package":"Sign = WXPay","partnerid":"1498191702","prepayid":"wx23131631764983da394a941c1058073000","sign_type":"HMAC - SHA256","timestamp":1590210991,"sign":"F9B89B33AC715AABE0C69AC825E1104BA283335731A01C1C7200C13420E65FFF"}"

          // var tempJson = JSON.parse(res.data.data.pay_parameters)
          // console.log(tempJson)
          // console.log(tempJson.appid)
          // var packages = "prepay_id=" + tempJson.prepayid
          // console.log(packages)
          // wx.requestPayment(
          //   {
          //     'timeStamp': '' +tempJson.timestamp,
          //     'nonceStr': '' + tempJson.noncestr,
          //     'package': "" + packages,
          //     'signType': 'MD5',
          //     'paySign': '' + tempJson.sign,
          //     'success': function (res) {
          //       console.log(res)
          //     },
          //     'fail': function (res) {
          //       console.log(res)
          //      },
          //     'complete': function (res) {
          //       console.log(res)
          //     }
          //   })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }

      }, fail: function () {

      }
    });
  },

  //待付款详情
  orderdetail: function (e) {
    wx.navigateTo({
      url: '/pages/mine/order-detail-2?order_id=' + e.currentTarget.dataset.id,
    })
  },
  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {

  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {

  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      orderList1:[]
    })
    this.getOrder1();
  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    console.log("滑到底部了")
    this.setData({
      page: this.data.page + 1
    })
    this.getOrder1();
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {

  }
});
